#include<cmath>
#include"vector.h"
namespace VECTOR
{
    using std::sqrt;
    using std::sin;
    using std::cos;
    using std::atan;
    using std::atan2;
    using std::cout;
    //计算1弧度的角度值
    const double Rad_to_deg=45.0/atan(1.0);
    void Vector::set_mag()
    {
        mag=sqrt(x*x+y*y);
    }
    void Vector::set_ang()
    {
        if (x==0&&y==0)
        {
            ang=0;
        }
        else
        {
            ang=atan2(y,x);
        }
    }
    //从极坐标设置x
    void Vector::set_x()
    {
        x=mag*cos(ang);
    }
    void Vector::set_y()
    {
        y=mag*sin(ang);
    }
    //公有方法
    //默认构造函数
    Vector::Vector()
    {
        x=y=mag=ang=0;
        mode=RECT;
    }
    Vector::Vector(double n1,double n2,Mode form)
    {
        mode=form;
        if (form==RECT)
        {
            x=n1;
            y=n2;
            set_mag();
            set_ang();
        }
        else if (form==POL)
        {
            mag=n1;
            ang=n2;
            set_x();
            set_y();
        }
        else
        {
            cout<<"对Vector，第三参数不正确";
            cout<<"vector设置为0\n";
            Vector();
        }
    }
    //如果参数为RECT（默认），以直角坐标系重置向量，或者参数为POL，以极坐标重置向量
    void Vector::reset(double n1,double n2,Mode form)
    {
        mode=form;
        if (form==RECT)
        {
            x=n1;
            y=n2;
            set_mag();
            set_ang();
        }
        else if (mode==POL)
        {
            mag=n1;
            ang=n2;
            set_x();
            set_y();
        }
        else
        {
            cout<<"对Vector，第三参数不正确";
            cout<<"vector设置为0\n";
            Vector();
        }
    }
    Vector::~Vector()
    {

    }
    void Vector::polar_mode()
    {
        mode=POL;
    }
    void Vector::rect_mode()
    {
        mode=RECT;
    }
    //运算符重载
    //将两个向量相加
    Vector Vector::operator+(const Vector &b)const
    {
        return Vector(x+b.x,y+b.y);
    }
    //从a中减去向量b
    Vector Vector::operator-(const Vector &b)const
    {
        return Vector(x-b.x,y-b.y);
    }
    //向量反号
    Vector Vector::operator-()const
    {
        return Vector(-x,-y);
    }
    //用n乘以向量vector
    Vector Vector::operator*(double n)const
    {
        return Vector(n*x,n*y);
    }
    //友元函数
    //向量a乘以乘以n
    Vector operator*(double n,const Vector &a)
    {
        return a*n;
    }
    //如果参数为RECT，显示直角坐标系；如果参数为POL，显示极坐标
    std::ostream & operator<<(std::ostream &os,const Vector &v)
    {
        if (v.mode==Vector::RECT)
        {
            os<<"(x,y)= ("<<v.x<<","<<v.y<<")";
        }
        else if (v.mode==Vector::POL)
        {
            os<<"(m,a)= ("<<v.mag<<","<<v.ang*Rad_to_deg<<")";
        }
        else
        {
            os<<"向量对象mode参数无效";
        }
        return os;
    }
}